package com.cskaoyan.controller;

import com.cskaoyan.bean.Storage;
import com.cskaoyan.bean.basebean.BaseParam;
import com.cskaoyan.bean.basebean.BaseRespVo;
import com.cskaoyan.bean.sysmanage.StorageVo;
import com.cskaoyan.bean.zhao_order_bean.BasePageData;
import com.cskaoyan.exception.MarketException;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.service.KeyWordService;
import com.cskaoyan.service.StorageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("admin/storage")
public class StorageController {
    @Autowired
    StorageService storageService;

    @Autowired
    AdminService adminService;

    @Autowired
    KeyWordService keyWordService;

    @RequiresPermissions(value = "admin:storage:list")
    @RequestMapping("list")
    public BaseRespVo getStorageList(String key, String name, BaseParam param) {
        BasePageData basePageData = storageService.queryStorage(key, name, param);
        return BaseRespVo.ok(basePageData);
    }

    //修改对象名称
    @RequiresPermissions(value = "admin:storage:update")
    @RequestMapping("update")
    public BaseRespVo updateStorageName(@RequestBody StorageVo storageVo) {
        StorageVo storage = storageService.updateStorageName(storageVo);
        return BaseRespVo.ok(storage);
    }

    //删除存储对象
    @RequiresPermissions(value = "admin:storage:delete")
    @RequestMapping("delete")
    public BaseRespVo deleteStorage(@RequestBody StorageVo storageVo) {
        boolean result = storageService.deleteStorage(storageVo);
        return result ? BaseRespVo.ok() : BaseRespVo.fail("删除失败");
    }

    //excel表上传
    @PostMapping("upload")
    public BaseRespVo create(MultipartFile file) throws MarketException {
        if (file.getSize() == 0 || file == null) {
            return BaseRespVo.fail("文件为空，请重新上传文件");
        }
        keyWordService.importKeywordData(file, keyWordService);
        return BaseRespVo.ok();
    }
}
